//
//  OrderDetailRequestAssist.m
//  KiklinkApp
//
//  Created by LF on 14-10-13.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "OrderDetailRequestAssist.h"
#import "API.h"

@implementation OrderDetailRequestAssist

-(instancetype)init
{
    if(self = [super init])
    {
        requestCount = 0;
    }
    return self;
}

-(void)singleRequestWithContent:(id)responseContent
{
    OrderItemPage *orderItemPage = [responseContent objectForKey:@"orderItemPage"];
    if(orderItemPage.hasNextPage)
    {
        NSNumber* nextPageNumber = [NSNumber numberWithInteger:orderItemPage.nextPageNumber];
        [API orderDetailWithOrderId:mOrderId PageNumber:nextPageNumber Callback:^(int stateCode, NSString *word, id content)
         {
             if(stateCode < 0)
             {
                 mCallback(stateCode, word, content);
             }
             else
             {
                 OrderItemPage *orderItemPage = [content objectForKey:@"orderItemPage"];
                 NSArray* orderItemArray = orderItemPage.result;
                 
                 OrderItemPage *returnOrderItemPage = [returnContent objectForKey:@"orderItemPage"];
                 NSArray* resultOrderItemArray = returnOrderItemPage.result;
                 
                 resultOrderItemArray = [resultOrderItemArray arrayByAddingObjectsFromArray:orderItemArray];
                 returnOrderItemPage.result = resultOrderItemArray;
                 
                 requestCount++;
                 if(requestCount > 3)
                 {
                     //调试用
                     //					 OrderItemPage *prober = [returnContent objectForKey:@"orderItemPage"];
                     
                     mCallback(FAILURE, @"订单中课程页数过多", returnContent);
                 }
                 else
                 {
                     [self singleRequestWithContent:content];
                 }
             }
         }
         ];
    }
    else
    {
        mCallback(SUCCESS, @"order detail all success", returnContent);
    }
}

-(void)startWithOrderId:(NSNumber*)orderId callback:(Callback)callback
{
    mOrderId = orderId;
    mCallback = callback;
    [API orderDetailWithOrderId:orderId PageNumber:@1 Callback:^(int stateCode, NSString *word, id content) {
        if(stateCode < 0)
        {
            callback(stateCode, word, content);
        }
        else
        {
            returnContent = content;
            [self singleRequestWithContent:content];
        }
    }];
}

@end
